package form;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.sun.lwuit.Image;
import helper.MobileDownloader;
import java.io.IOException;
import helper.Util;
import java.util.Timer;
import java.util.TimerTask;

import midlet.MainMidlet;
import model.ItemMenu;

/**
 *
 * @author Hallucinogen, LegACy
 */
public class SplashForm extends ParentForm {

    public SplashForm(MainMidlet midlet, ParentForm parent) {
        super(midlet, parent, null);

        String URL = null;
        Image bg = null;
        try {
            URL = Util.getInstance().getBackgroung("splash");
            bg = Image.createImage(MobileDownloader.download(URL), 0, MobileDownloader.download(URL).length);
        } catch (Exception ex) {
            try {
                bg = Image.createImage(parent.SPLASH_IMAGE);
            } catch (IOException ex1) {
                ex1.printStackTrace();
            }
        }

        this.getStyle().setBgImage(bg);

        //Download stuff here instead of using timer
        timer.schedule(new CountDown(), 1000);
    }

    private void dismiss() {
        //Load item file
        String[] array = null;
        try {
            array = Util.split(Util.getInstance().getMenuByParentId(Util.PARENT_MENU), "####");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        System.out.println("jumlah array: " + array.length);

        ItemMenu[] ItemList = new ItemMenu[array.length + 1];
        for (int i = 0; i < array.length - 1; i++) {
            String[] data = Util.split((String) array[i], "|");
            Image icon = Image.createImage(MobileDownloader.download(data[3]), 0, MobileDownloader.download(data[3]).length);
            ItemList[i] = new ItemMenu(Integer.parseInt(data[0]), data[1], data[2], icon, Integer.parseInt(data[4]));
            System.out.println("item At[" + i + "] =" + array[i]);

        }
        Image exit = null;
        Image home = null;
        try {
            home = Image.createImage("/res/image/icon/home.png");
            exit = Image.createImage("/res/image/icon/exit.png");
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }

        //add Staict Menu
//        ItemList[array.length - 1] = new ItemMenu("TNC", "text", 0);
//        ItemList[array.length] = new ItemMenu("Exit", "exit", 0);
         ItemList[array.length - 1] = new ItemMenu(0, "T n C", "text", home, 0);
        ItemList[array.length] = new ItemMenu(0,"Exit", "exit",exit, 0);

        //Create main form
        new MainFormUi(this.midlet, null, ItemList, null).show();
        this.disposeForm();
    }

    private class CountDown extends TimerTask {

        public void run() {
            dismiss();
        }
    }
    private Timer timer = new Timer();
}
